package com.zhangh.summer.admin.support.utils;

import com.zhangh.summer.admin.support.exception.BizException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2021-09-16 14:04
 **/
public class ExcelTool {

    public static List<Map<Integer,String>> handler(InputStream is){
        List<Map<Integer,String>> list=new ArrayList<>();
        Workbook workBook = null;
        try {
            workBook = new HSSFWorkbook(is);
            Sheet sheet = workBook.getSheetAt(0);
            int rowIndex=0;
            int columnIndex=0;
            for (Row cells : sheet) {
                if (rowIndex>0){
                    try {
                        Map<Integer,String> cellMap=handlerCells(cells);
                        list.add(cellMap);
                    }catch (Exception e){
                        //e.printStackTrace();
                        throw BizException.exception("解析Excel异常 {0}  异常行{1}列{2}",e.getMessage(),rowIndex+1,columnIndex+1);
                    }
                }
                rowIndex++;
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw BizException.exception("解析文件异常！{0}",e.getMessage());
        } finally{
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(workBook != null){
                try {
                    workBook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

   /* private static void dealWorkBook(Workbook workBook,Class ... cellTypes){
        Sheet sheet = workBook.getSheetAt(0); // 获取第一个sheet
        int rowIndex=0;
        int columnIndex=0;
        for (Row cells : sheet) {
            if (rowIndex>0){
                try {
                    Map<Integer,String> cellMap=handlerCells(cells);

                }catch (Exception e){
                    //e.printStackTrace();
                    throw BizException.exception("解析Excel异常 {0}  异常行{1}列{2}",e.getMessage(),rowIndex+1,columnIndex+1);
                }
            }
            rowIndex++;
        }
    }*/
    private static Map<Integer,String> handlerCells(Row cells){
        Map<Integer,String> hashMap=new HashMap<>();
        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
        Integer index=0;
        for (Cell cell : cells) {
            String value=null;
            switch(cell.getCellType()) {
                case STRING:
                    value=cell.getRichStringCellValue().getString();
                    break;
                case _NONE:
                    value=cell.getRichStringCellValue().getString();
                    break;
                case NUMERIC:
                    value=decimalFormat.format(cell.getNumericCellValue());
                    break;
                case BOOLEAN:
                    boolean cellValue=cell.getBooleanCellValue();
                    if (cellValue){
                        value="true";
                    }else{
                        value="false";
                    }
                    break;
                case FORMULA:
                    value=decimalFormat.format(cell.getNumericCellValue());
                    break;
                case BLANK:
                    break;
                case ERROR:
                    break;
            }

            hashMap.put(index,value);
            index++;
        }
        return hashMap;
    }
    private String objToString(Object obj){
        if (obj instanceof String){
            return obj.toString();
        }
        if (obj instanceof Double){
            //String str=obj.toString();
            DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            return decimalFormat.format(obj);
        }
        return (String)obj;
    }

}
